<?php

App::uses('Crud', 'Model');
App::uses('Moe', 'Model');
App::uses('Area', 'Model');
App::uses('User', 'Model');

class Manager extends Crud
{
    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const MANAGER_RH = 1;
    const MANAGER_MOE = 2;
    const TRAINING_SUPERVISOR = 3;

    /**
     * Obtenemos los registros para el grid del admin
     * @return type
     */
    public function getEnabled() {
        $filters = array(
            'fields' => array(
                'Manager.id as value',
                'm.id as moeId',
                'u.id as userId',
                'm.name as moeN',
                'u.full_name as userN',
                'Manager.signature as sign',
                'Manager.job as job',
                'Manager.type as type'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = Manager.user_id',
                        'u.status' => User::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'left',
                    'conditions' => array(
                        'm.id = Manager.moe_id',
                        'm.status' => Moe::STATUS_ENABLED,
                    ),
                )
            ),
            'conditions' => array(
                'Manager.status' => self::STATUS_ENABLED
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function haveRHManager() {
        $filters = array(
            'fields' => array(
                'Manager.id',
            ),
            'conditions' => array(
                'Manager.type = 1',
                'Manager.status' => self::STATUS_ENABLED
            ),
        );
        return $this->find('first', $filters);
    }
}
